package com.atguigu.eduservice.service.impl;

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.event.AnalysisEventListener;
import com.atguigu.eduservice.entity.EduSubject;
import com.atguigu.eduservice.entity.excel.SubjectData;
import com.atguigu.eduservice.entity.subject.OneSubject;
import com.atguigu.eduservice.entity.subject.TwoSubject;
import com.atguigu.eduservice.listener.SubjectExcelListener;
import com.atguigu.eduservice.mapper.EduSubjectMapper;
import com.atguigu.eduservice.service.EduSubjectService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 课程科目 服务实现类
 * </p>
 *
 * @author wangshuai
 * @since 2021-02-03
 */
@Service
public class EduSubjectServiceImpl extends ServiceImpl<EduSubjectMapper, EduSubject> implements EduSubjectService {

    //添加课程分类
    @Override
    public void saveSubject(MultipartFile file, EduSubjectService subjectService) {

        try {
            InputStream stream = file.getInputStream();
            EasyExcel.read(stream, SubjectData.class, new SubjectExcelListener(subjectService)).sheet().doRead();
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

    @Override
    public List<OneSubject> getAllOneTwoSubject() {

        //获取一级分类二级分类数据
        //一级分类
        QueryWrapper<EduSubject> wrapperOne = new QueryWrapper<>();

        wrapperOne.eq("parent_id", "0");

        List<EduSubject> oneSubjectList = baseMapper.selectList(wrapperOne);

        //二级分类
        QueryWrapper<EduSubject> wrapperTwo = new QueryWrapper<>();

        wrapperTwo.ne("parent_id", "0");

        List<EduSubject> twoSubjecctList = baseMapper.selectList(wrapperTwo);

        //最后返回的集合
        List<OneSubject> oneRecordsList = new ArrayList<>();

        //封装一级分类
        for (int i = 0; i < oneSubjectList.size(); i++) {

            EduSubject eduSubjectOne = oneSubjectList.get(i);

            OneSubject oneSubject = new OneSubject();

            BeanUtils.copyProperties(eduSubjectOne, oneSubject);

            oneRecordsList.add(oneSubject);

            //封装二级分类
            ArrayList<TwoSubject> twoRecordsList = new ArrayList<>();

            for (int i1 = 0; i1 < twoSubjecctList.size(); i1++) {

                EduSubject eduSubjectTwo = twoSubjecctList.get(i1);

                if(eduSubjectTwo.getParentId().equals(eduSubjectOne.getId())) {

                    TwoSubject twoSubject = new TwoSubject();

                    BeanUtils.copyProperties(eduSubjectTwo, twoSubject);

                    twoRecordsList.add(twoSubject);

                }

            }

            oneSubject.setChildren(twoRecordsList);

        }

        System.out.println();
        return oneRecordsList;
    }
}
